package tools;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

@Data
@NoArgsConstructor
public class VerificationCodeBuilder {
    private String resultCode;
    private BufferedImage resultImage;

    private int width;
    private int height;
    private int codeCount;

    public void generateCodeAndImage() {
        int x = width / (codeCount + 1);
        int y = height - 4;
        char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
                'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
                'j', 'k', 'm', 'n', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y'};
        BufferedImage bufferedImage = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bufferedImage.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, this.width, this.height);
        Font font = new Font("Fixedsys", Font.PLAIN, this.height - 7);
        g.setFont(font);
        g.setColor(Color.WHITE);
        g.drawRect(0, 0, this.width - 2, this.height - 2);
        StringBuffer randomCode = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < this.codeCount; i++) {
            String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
            int red = random.nextInt(255);
            int green = random.nextInt(255);
            int blue = random.nextInt(255);
            g.setColor(new Color(red, green, blue));
            g.drawString(strRand, (i + 1) * x, y - 2);
            randomCode.append(strRand);
        }
        this.resultCode = randomCode.toString();
        this.resultImage = bufferedImage;
    }
}
